package com.th.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.th.Service.GoodsService;
import com.th.pojo.Goods;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/authc")
public class GoodsController {

    private static final Logger LOGGER = LoggerFactory.getLogger(GoodsController.class);

    @Autowired
    GoodsService goodsService;

    @GetMapping("/goodslist")
    public String list(Model model, @RequestParam(value = "pagenum", defaultValue = "1") String pageNum) {
        //查询全部
        IPage<Goods> page = goodsService.page(new Page<>(Integer.parseInt(pageNum), 10));
        model.addAttribute("page", page);
        return "authc/goods";
    }

    @GetMapping("/delete")
    @ResponseBody
    public String delete(String id) {
        Boolean result = goodsService.removeById(Integer.parseInt(id));
        if (result) {
            return "1";
        }
        return "0";
    }

    @GetMapping("/batchdelete")
    @ResponseBody
    public String batchdelete(@RequestParam("ids") String batchid) {
        String[] ids = batchid.split(",");
        List<Long> list = new ArrayList<>();
        for (String id : ids) {
            list.add(Integer.parseInt(id) + 0L);
        }
        boolean result = goodsService.removeByIds(list);
        if (result) {
            return "1";
        }
        return "0";
    }

    /**
     *
     * @param goodsnumber 货物编码
     * @param name 货物名称
     * @param vehicletype 车型
     * @param unit 单位
     * @param buyprice 进价
     * @param sellprive 售价
     * @param supplier 供货商
     * @param stockadd 进货数
     * @return
     */
    @PostMapping("/goodsadd")
    public String goodsadd(String goodsnumber, String name, String vehicletype, String unit, String buyprice, String sellprive,
                           String supplier, String stockadd) {

        return "0";
    }

    //检测货物是否已存在
    @GetMapping("/checkgoodsnumber")
    @ResponseBody
    public String checkgoodsnumber(String goodsnumber){
        String json = ""; //返回的JSON字符串
        LambdaQueryWrapper<Goods> queryWrapper = Wrappers.<Goods>lambdaQuery().eq(Goods::getGoodsnumber, goodsnumber);
        Goods goods = goodsService.getOne(queryWrapper);
        if (goods == null) {
            json = "yes";
        } else {
            json = "no";
        }
        return json;
    }

}
